import coin.exchange.base.kr_rest.native_public_client_base as npubcb
from coin.exchange.binance.kr_rest.constants import symbol_list


class BinanceNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.binance.com/api/v1")
    self._valid_symbol_type = symbol_list
    self._valid_kline_period = ['1m', '3m', '5m', '15m', '30m', \
        '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M']

  def check_symbol(self, symbol):
    assert symbol in self._valid_symbol_type, \
        'Invalid contract type!'

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
        'Invalid kline period!'

  def get_ticker(self, symbol=None):
    params = dict(symbol=symbol) if symbol else None
    return self._get_as_json("ticker/price", params=params)

  def get_orderbook(self, symbol=None):
    params = dict(symbol=symbol) if symbol else None
    return self._get_as_json("depth", params=params)

  def get_history_kline(self, symbol, kline_period, start_time, end_time):
    self.check_kline_period(kline_period)
    url = "klines"
    params = {
        'symbol': symbol,
        'interval': kline_period,
        'startTime': int(start_time.timestamp() * 10**3),
        'endTime': int(end_time.timestamp() * 10**3)
    }
    return self._get_as_json(url, params)

  def queryAll(self):
    return get_orderbook()


if __name__ == "__main__":
  client = BinanceNativePublicClient()
  print(client.get_ticker())
  print(client.get_orderbook())
  print(client.get_history_kline('BTCUSDT', '1h', 100))
